<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
class CouponController extends AdminBaseController
{
    public function index()
    {   
        $where = array();
        $sessionAdminId = session('ADMIN_ID');
        $status = 0;
        if($sessionAdminId !== 1){
            //说明是店铺登陆  只查询本店铺发布的优惠券
            $find = db::name('app_shop')->where('admin_id',$sessionAdminId)->find();
            $where[] = ['shop_id','eq',$find['id']];
        }

        $params = $this->param();


        if(isset($params['name']) && $params['name']){
            $map[] = ['name','like','%'.$params['name'].'%'];
            $find = db::name('app_shop')->where($map)->find();
            $where[] = ['shop_id','eq',$find['id']];
        }
        if(isset($params['status']) && $params['status']){
            $where[] = ['status','eq',$params['status']];
            $status = $params['status'];
        }

        $coupon = db::name('app_coupon')->where($where)
            ->paginate(10,false,
            ['query'=>$params])->each(function($item, $key){
            if($item['shop_id']){
                $item['shopname'] = db::name('app_shop')->where('id',$item['shop_id'])->value('name');
            }else{
                $item['shopname'] = '平台发布';
            }
            return $item;
        });


        $this->assign('status',$status);
        $this->assign('sessionAdminId',$sessionAdminId);
        $this->assign('coupon',$coupon);
        $coupon->appends($params);  //分页条件
        $this->assign('page', $coupon->render());
        return $this->fetch();
    }

    public function add()
    {
        if($_POST){
            $validate = new Validate([
                'full'  => 'require',
                'reduce'  => 'require',
                'remark'  => 'require',
                'stime'  => 'require',
                'etime'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            $sessionAdminId = session('ADMIN_ID');
            if($sessionAdminId !== 1){
                //说明是店铺登陆  只查询本店铺发布的优惠券
                $find = db::name('app_shop')->where('admin_id',$sessionAdminId)->find();
                $_POST['shop_id'] = $find['id'];
            }
            $res = db::name('app_coupon')->insert($_POST);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }

        return $this->fetch();
    }
    public function edit()
    {
        $id = $this->param('id');
        if($_POST){
            $validate = new Validate([
                'full'  => 'require',
                'reduce'  => 'require',
                'remark'  => 'require',
                'stime'  => 'require',
                'etime'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $res = db::name('app_coupon')->update($_POST);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }

        }else{
            $find = db::name('app_coupon')->find($id);
            $this->assign('info',$find);
            return $this->fetch();
        }
    }
    //店铺商品
    public function lst()
    {
        $where = array();
        $params = $this->param();
        $type = 0;
        if(isset($params['id'])){
            $where[] = ['t1.coupon_id','eq',$params['id']];
        }

        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆  只查询本店铺发布的优惠券
            $idarr = db::name('app_shop')->alias('t1')
                ->join('app_coupon t2','t2.shop_id = t1.id')
                ->where('t1.admin_id',$sessionAdminId)
                ->column('t2.id');
            $where[] = ['t3.shop_id','in',$idarr];
        }

        if(isset($params['name']) && $params['name']){
            $map[] = ['name','like','%'.$params['name'].'%'];
            $find = db::name('app_shop')->where($map)->find();
            $where[] = ['t3.shop_id','eq',$find['id']];
        }
        if(isset($params['type']) && $params['type']){
            $where[] = ['t1.type','eq',$params['type']];
            $type = $params['type'];
        }

        $list = db::name('app_coupon_list')->alias('t1')
            ->join('app_user t2','t2.id = t1.uid')
            ->join('app_coupon t3','t3.id = t1.coupon_id')
            ->field('t1.id,t1.type,t2.nickname,t3.full,t3.reduce,t3.shop_id,t3.remark,t1.addtime')
            ->where($where)
            ->paginate(10,false,['query'=>$params])->each(function($item, $key){
                if($item['shop_id']){
                    $item['shopname'] = db::name('app_shop')->where('id',$item['shop_id'])->value('name');
                }else{
                    $item['shopname'] = '平台发布';
                }
                return $item;
            });
        $this->assign('sessionAdminId',$sessionAdminId);
        $this->assign('list',$list);
        $this->assign('type',$type);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
}
